package com.rongshu.sendmessage.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.wifi.aware.DiscoverySession;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.gyf.immersionbar.ImmersionBar;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.base.MyApplication;
import com.rongshu.sendmessage.util.StringUtils;
import com.rongshu.sendmessage.util.httpUtil.HYContent;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 欢迎进入首页 友来直信
 */
public class WelComeActivity extends BaseActivity {

    public static final int AD_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        ImmersionBar.with(this)
                .statusBarColor(R.color.transparent)
                .statusBarDarkFont(true)
                .init();
        setContentView(R.layout.activity_wel_come);
        ImageView iv_logo=findViewById(R.id.iv_logo);
        String image = MyApplication.mSettings.getString(HYContent.AppStartPageImg, "");
        if (StringUtils.isEmpty(image)){
            iv_logo.setImageResource(R.mipmap.qidong_icon);
        }else{
            ImageLoader.getInstance().displayImage(image,iv_logo);
        }
        MyApplication.editor.putString(HYContent.CITY_LOCATION_NAME,"");
        MyApplication.editor.putString(HYContent.CITY_NAME,"");
        MyApplication.editor.commit();
        startTime();
    }

    private void startTime() {
        Timer timer=new Timer();
        TimerTask task=new TimerTask() {
            @Override
            public void run() {
                Message msg=new Message();
                msg.what=2;
                msg.obj=(0)+"";
                handler.sendMessage(msg);
            }
        };
        timer.schedule(task,AD_TIME_OUT);
    }

    @SuppressLint("HandlerLeak")
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==2){
                goToMainActivity();
            }
        }
    };

    private void goToMainActivity() {
        boolean isLogin = MyApplication.mSettings.getBoolean(HYContent.IS_LOGIN, false);
        Intent intent;
        if(isLogin){
            intent = new Intent(mContext, MainActivity.class);
        }else{
            intent = new Intent(mContext, HYLoginActivity.class);
        }
        startActivity(intent);
        finish();

    }

}